const SMSClient = require('@alicloud/sms-sdk')

// ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
const accessKeyId = 'LTAIUgGz2owtqtis'
const secretAccessKey = 'GrrcnEEP9VEfUJEOfbxHTg0ca2632i'

//在云通信页面开通相应业务消息后，就能在页面上获得对应的queueName,不用填最后面一段
const queueName = 'Alicom-Queue-1911904504058670'

//初始化sms_client
let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
/* 
smsClient.sendBatchSMS({
   PhoneNumberJson: JSON.stringify(['18040580000', '15088650000']),
   SignNameJson: JSON.stringify(['短信迁移测试签名','短信迁移测试签名']),
   TemplateCode: 'SMS_71175823',
   TemplateParamJson: JSON.stringify([{code: "1234", product: "ytx1"}, {code: "5678", product: "ytx2"}]),
}).then(function (res) {
   let {Code}=res
   if (Code === 'OK') {
      //处理返回参数
      console.log(res)
   }
}, function (err) {
   console.log('err', err)
})
 
 
//短信回执报告
smsClient.receiveMsg(0, queueName).then(function (res) {
   //消息体需要base64解码
   let {code, body}=res
   if (code === 200) {
       //处理消息体,messagebody
       console.log(body)
   }
}, function (err) {
   console.log(err)
})
 
//短信上行报告
smsClient.receiveMsg(1, queueName).then(function (res) {
   //消息体需要base64解码
   let {code, body}=res
   if (code === 200) {
       //处理消息体,messagebody
       console.log(body)
   }
}, function (err) {
   console.log(err)
})
 */

// //查询短信发送详情
// smsClient.queryDetail({
//     PhoneNumber: '17625493610',
//     SendDate: '20170731',
//     PageSize: '10',
//     CurrentPage: "1"
// }).then(function (res) {
//     let {Code, SmsSendDetailDTOs}=res
//     if (Code === 'OK') {
//         //处理发送详情内容
//         console.log(SmsSendDetailDTOs)
//     }
// }, function (err) {
//     //处理错误
//     console.log(err)
// })

//发送短信
const sendMsg = async ({ PhoneNumbers, code }) => {
    if (PhoneNumbers.length > 0) {
        const res = await smsClient.sendSMS({
            PhoneNumbers,
            SignName: '车盟网络',
            TemplateCode: 'SMS_135765161',
            TemplateParam: JSON.stringify({ code })
        }), { Code } = res;

        if (Code === 'OK') {
            //处理返回参数
            return res;
        }
        throw new Error("短信处理失败");
    } else {
        throw new Error("手机号不能为空");
    }
};

export {
    sendMsg
}